<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Inspection\Repository\Inspection_plans;

use Plugin\Attractions\Inspection\Model\Inspection_plans\InspectionPlans;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 日常巡检计划 Repository类
 */
class InspectionPlansRepository extends IRepository
{
   public function __construct(
        protected readonly InspectionPlans $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        if (isset($params['name']) && !empty($params['name'])) {
            $query->where('name', 'like', '%' . $params['name'] . '%');
        }

        // 巡检周期 1每日、2每周、3每月
        if (isset($params['cycle']) && !empty($params['cycle'])) {
            $query->where('cycle', '=', $params['cycle']);
        }

        // 巡检人
        if (isset($params['person']) && !empty($params['person'])) {
            $query->where('person', '=', $params['person']);
        }

        // 巡检设备
        if (isset($params['devices']) && !empty($params['devices'])) {
            $query->where('devices', '=', $params['devices']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}